package com.acce.system.controller;

import java.io.File;
import java.security.NoSuchAlgorithmException;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.acce.base.controller.BaseController;
import com.acce.base.pojo.ArgsPojo;
import com.acce.base.pojo.ReturnCode;
import com.acce.base.pojo.ReturnPojo;
import com.acce.base.utils.PropertiesUtil;
import com.acce.base.utils.TokenUtils;
import com.acce.system.config.ReturnCodes;
import com.acce.system.pojo.LoginLog;
import com.acce.system.pojo.UserList;
import com.acce.system.service.ILogService;
import com.acce.system.service.IUserService;
import com.alibaba.fastjson.JSONObject;

@Controller
public class AuthController extends BaseController {
	
	public static String basepath;
	
	@Resource
	private IUserService userService;
	
	@Resource
	private ILogService logService;
	
	@RequestMapping("/api/auth")
	public void center(@ModelAttribute("argsPojo")ArgsPojo args, HttpServletRequest request, HttpServletResponse response, HttpSession httpSession){
		String serviceCode = args.getServiceCode();
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		basepath = request.getSession().getServletContext().getRealPath(File.separator + "upload");
		//验证基础参数是否缺少
		if(isEmptyHead(args)){
			logger.info("system-core::center::isEmptyHead ");
			rp.setServiceCode(args.getServiceCode());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			writeJsonToResponse(response,JSONObject.toJSON(rp).toString());
			return;
		}
		//特殊处理
		if(!"1001W04".equals(serviceCode) || !"MMZS8001".equals(serviceCode)){
			if(StringUtils.isEmpty(args.getBusiness())){
				ReturnCode rc = userService.getReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				writeJsonToResponse(response, JSONObject.toJSON(rp).toString());
				return;
			}
			String token;
			if("5001U01".equals(serviceCode) || "5001U02".equals(serviceCode)
					|| "5002U01".equals(serviceCode) || "1001W02".equals(serviceCode)
					|| "1001W03".equals(serviceCode) || "5004U02".equals(serviceCode)){ //使用固定token的服务
				token = "411e7bc272a421b44802a9f608a740e5ca74872a";
			}else{
				ReturnPojo rpLogin = logService.loadToken(args);
				String recode = rpLogin.getReturnCode();
				if(!"200".equals(recode)){
					rp.setReturnCode(rpLogin.getReturnCode());
					rp.setReturnMsg(rpLogin.getReturnMsg());
					writeJsonToResponse(response, JSONObject.toJSON(rp).toString());
					return;
				}
				LoginLog loginLog =  JSONObject.parseObject(JSONObject.toJSONString(rpLogin.getResult()), LoginLog.class);
				token = loginLog.getToken();
			}
			
			if(StringUtils.isEmpty(token)){
				ReturnCode rc = userService.getReturnCode(ReturnCodes.TOKEN_INVALID.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				writeJsonToResponse(response, JSONObject.toJSON(rp).toString());
				return;
			}
			logger.info("nsy-auth::center::get token " + token);
			String sign = null;
			if(StringUtils.isEmpty(args.getBusiness())){
				ReturnCode rc = userService.getReturnCode(ReturnCodes.PARAM_NULL.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				writeJsonToResponse(response, JSONObject.toJSON(rp).toString());
				return;
			}
			try {
				sign = TokenUtils.getSign(token, args.getTimestamp(), args.getNonce());
			} catch (NoSuchAlgorithmException e) {
				logger.info("nsy-auth::center::" + e.getMessage());
				ReturnCode rc = userService.getReturnCode(ReturnCodes.SIGN_MAKE_FAIL.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				writeJsonToResponse(response, JSONObject.toJSON(rp).toString());
				return;
			}
			logger.info("nsy-auth::center::get sign " + sign);
			if(!sign.equals(args.getSign())){
				ReturnCode rc = userService.getReturnCode(ReturnCodes.SIGN_VALID_FAIL.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				writeJsonToResponse(response, JSONObject.toJSON(rp).toString());
				return;
			}
		}
		
		//开始服务
		if("5001U01".equals(serviceCode)){
			rp = checkNumRepeat(args, response);  //个人查重
		}else if("5001U02".equals(serviceCode)){
			rp = registerUser(args, response);  //注册用户
		}else if("5001U11".equals(serviceCode)){
			rp = checkCompanyRepeat(args, response); //公司查重
		}else if("5001U12".equals(serviceCode)){
			rp = registerCompany(args, response);  //注册公司
		}else if("5002U01".equals(serviceCode)){
			rp = login(args, request, response);  //登录
		}else if("5002U02".equals(serviceCode)){
			rp = logout(args, response);  //登出
		}else if("5003U01".equals(serviceCode)){
			rp = queryUserInfo(args, response);  //查个人信息
		}else if("5003U02".equals(serviceCode)){
			rp = resetUserInfo(args, response);  //重置个人信息
		}else if("5003U03".equals(serviceCode)){
			rp = getUpLoadToken(args, response);  //获取七牛token
		}else if("5003U04".equals(serviceCode)){
			rp = downLoad(args, response);  //下载七牛图片
		}else if("5003U11".equals(serviceCode)){
			rp = queryCompanyInfo(args, response);  //查询公司信息
		}else if("5003U12".equals(serviceCode)){
			rp = resetCompanyInfo(args, response);  //重置公司信息
		}else if("5003U13".equals(serviceCode)){
			rp = addDeliverAddress(args, response);  //添加收发货地址
		}else if("5003U14".equals(serviceCode)){
			rp = updateDeliverAddress(args, response);  //添加收发货地址
		}else if("5004U01".equals(serviceCode)){
			rp = resetPwd(args, response);  
		}else if("5004U02".equals(serviceCode)){
			rp = findPwd(args, response);  
		}else if("5005U01".equals(serviceCode)){
			rp = queryOrganizaStruct(args, response);  //查询组织架构
		}else if("5005U02".equals(serviceCode)){
			rp = addDepartment(args, response);  //添加部门
		}else if("5005U03".equals(serviceCode)){
			rp = deleteDepartment(args, response);  //删除部门
		}else if("5005U04".equals(serviceCode)){
			rp = renameDepartment(args, response);  //重命名部门
		}else if("5006U04".equals(serviceCode)){
			rp = queryVagueCompany(args, response);  //模糊查询公司
		}else if("5006U05".equals(serviceCode)){
			rp = existsCompany(args, response);  //公司存在
		}else if("5006U06".equals(serviceCode)){
			rp = joinCompany(args, response);  //加入公司审批
		}else if("5006U07".equals(serviceCode)){
			rp = joinAuditPass(args, response);  //加入公司
		}else if("5006U08".equals(serviceCode)){
			rp = exitCompany(args, response);  //离开公司
		}else if("5009U01".equals(serviceCode)){
			rp = queryCompanyRelation(args, response);  //查询公司的供应商
		}else if("5009U02".equals(serviceCode)){
			rp = queryAllRelation(args, response);  //查询所有供应商
		}else if("5009U03".equals(serviceCode)){
			rp = addCompanyRelation(args, response);  //添加供应商
		}else if("5009U04".equals(serviceCode)){
			rp = getCompanyAllEmployee(args, response);  //获取公司所有员工
		}else if("5009U05".equals(serviceCode)){
			rp = deleteRelations(args, response);  //删除渠道
		}else if("1001W02".equals(serviceCode)){
			rp = autoLoginWX(args, response);  //自动登录
	    }else if("1001W03".equals(serviceCode)){
			rp = checkOpenIdBinding(args, response); //检查openId是否绑定
	    }else if("1001W04".equals(serviceCode)){
			rp = checkOtherLogin(args, response);
		}else if("MMZS8001".equals(serviceCode)){
			rp = checkAppVersion(args, response);  //检测app版本
		}
		
		writeJsonToResponse(response, JSONObject.toJSON(rp).toString());
	}
	
	/***
	 * 账号查重
	 * @param args
	 * @param response
	 */
	private ReturnPojo checkNumRepeat(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.checkNumRepeat(args);
		} catch (Exception e) {
			logger.error("nsy-auth::checkNumRepeat::checkNum::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.QUERY_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 注册个人用户
	 * @param args
	 * @param response
	 */
	private ReturnPojo registerUser(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.registerUser(args);
		} catch (Exception e) {
			logger.error("nsy-auth::registerUser::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.REGISTER_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/***
	 * 公司名查重
	 * @param args
	 * @param response
	 */
	private ReturnPojo checkCompanyRepeat(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.checkCompanyRepeat(args);
			logger.info("nsy-auth::checkCompanyRepeat::check company end");
		} catch (Exception e) {
			logger.error("nsy-auth::checkCompanyRepeat::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.QUERY_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 注册企业
	 * @param args
	 * @param response
	 */
	private ReturnPojo registerCompany(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.registerCompany(args);
		} catch (Exception e) {
			logger.error("nsy-auth::registerCompany::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.REGISTER_COMPANY_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 登录
	 * @param args
	 * @param response
	 */
	private ReturnPojo login(ArgsPojo args, HttpServletRequest request, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.login(args);
			
			if(ReturnCodes.SUCCESS.getCode().equals(rp.getReturnCode())) {
				JSONObject json = (JSONObject)rp.getResult();
				UserList userList = (UserList)json.get("userList");
				request.getSession().setAttribute("user", userList);  
	            
				String qwebuer = userList.getAtUserId();
	            //向客户端写入登录用户id
	            Cookie cu = new Cookie("qwebuer", qwebuer);
	            cu.setMaxAge(3600*24);//1天
	            cu.setDomain(".accesale.com");//www.accesale.com设置域
	            cu.setPath("/");//当前的web服务器下的所有.accesale.com

				String token = json.getString("token");
	            //向客户端写入token
	            Cookie ct = new Cookie("qwebuer", token);
	            ct.setMaxAge(3600*24);//1天
	            ct.setDomain(".accesale.com");//www.accesale.com设置域
	            ct.setPath("/");//当前的web服务器下的所有.accesale.com
	            response.addCookie(cu);
	            response.addCookie(ct);
			}
		} catch (Exception e) {
			logger.error("nsy-auth::login::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.LOGIN_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 微信自动登录
	 * @param args
	 * @param response
	 */
	private ReturnPojo autoLoginWX(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			logger.error("nsy-auth::autoLoginWX::call autoLoginWX service");
			rp = userService.autoLoginWX(args);
			logger.error("nsy-auth::autoLoginWX::return code::" + rp.getReturnCode());
			logger.error("nsy-auth::autoLoginWX::return msg::" + rp.getReturnMsg());
		} catch (Exception e) {
			logger.error("nsy-auth::autoLoginWX::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.LOGIN_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * openId绑定查重
	 * @param args
	 * @param response
	 */
	private ReturnPojo checkOpenIdBinding(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			logger.error("nsy-auth::checkOpenIdBinding::call checkOpenIdBinding service");
			rp = userService.checkOpenIdBinding(args);
			logger.error("nsy-auth::checkOpenIdBinding::return code::" + rp.getReturnCode());
			logger.error("nsy-auth::checkOpenIdBinding::return msg::" + rp.getReturnMsg());
		} catch (Exception e) {
			logger.error("nsy-auth::checkOpenIdBinding::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.OPENID_BINDING_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 检查其他设备登录,此方法属于特殊处理，有待优化
	 * @param args
	 * @param response
	 */
	private ReturnPojo checkOtherLogin(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		ReturnPojo rpLogin = logService.loadTokenNoVali(args);
		String recode = rpLogin.getReturnCode();
		if(!"200".equals(recode)){
			rp.setReturnCode(rpLogin.getReturnCode());
			rp.setReturnMsg(rpLogin.getReturnMsg());
			return rp;
		}
		LoginLog loginLog =  JSONObject.parseObject(JSONObject.toJSONString(rpLogin.getResult()), LoginLog.class);
		String token = loginLog.getToken();
		if(StringUtils.isEmpty(token)){
			ReturnCode rc = userService.getReturnCode(ReturnCodes.TOKEN_INVALID.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		logger.info("nsy-auth::checkOtherLogin::get token " + token);
		String sign = null;
		try {
			sign = TokenUtils.getSign(token, args.getTimestamp(), args.getNonce());
		} catch (NoSuchAlgorithmException e) {
			logger.info("nsy-auth::checkOtherLogin::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.SIGN_MAKE_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		logger.info("nsy-auth::checkOtherLogin::get sign " + sign);
		if(sign.equals(args.getSign())){ //一致说明未被挤
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		}else{  //已挤
			ReturnCode rc = userService.getReturnCode(ReturnCodes.OTHER_DEVICE_LOGIN.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 查询个人信息
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo queryUserInfo(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.queryUserInfo(args);
		} catch (Exception e) {
			logger.error("nsy-auth::queryUserInfo::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.QUERY_USER_INFO_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	private ReturnPojo logout(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			userService.logout(args);
		} catch (Exception e) {
			ReturnCode rc = userService.getReturnCode(ReturnCodes.LOGINOUT_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 修改个人信息
	 * @param args
	 * @param response
	 */
	private ReturnPojo resetUserInfo(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.resetUserInfo(args);
		} catch (Exception e) {
			logger.error("nsy-auth::resetUserInfo::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.RESET_USER_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 查询公司渠道供应商
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo queryCompanyRelation(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.queryCompanyRelation(args);
		} catch (Exception e) {
			logger.error("nsy-auth::queryCompanyRelation::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.QUERY_COMPANY_RELATION_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 查询某地区所有渠道供应商
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo queryAllRelation(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.queryAllRelation(args);
		} catch (Exception e) {
			logger.error("nsy-auth::queryAllCompany::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.QUERY_COMPANY_RELATION_CITY_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 添加渠道供应商
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo addCompanyRelation(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.addCompanyRelation(args);
		} catch (Exception e) {
			logger.error("nsy-auth::addCompanyRelation::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.ADD_COMPANY_RELATION_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 获取公司所有员工
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo getCompanyAllEmployee(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.getCompanyAllEmployee(args);
		} catch (Exception e) {
			logger.error("nsy-auth::getCompanyAllEmployee::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.QUERY_COMPANY_ALL_EMPLOYEE_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 删除渠道
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo deleteRelations(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.deleteRelations(args);
		} catch (Exception e) {
			logger.error("nsy-auth::deleteRelations::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.QUERY_COMPANY_ALL_EMPLOYEE_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	public ReturnPojo getUpLoadToken(ArgsPojo args, HttpServletResponse response) {
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.getUpLoadToken(args);
		} catch (Exception e) {
			logger.error("nsy-auth::getUpLoadToken::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.RESET_USER_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 模糊查询公司
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo queryVagueCompany(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.queryVagueCompany(args);
		} catch (Exception e) {
			logger.error("nsy-auth::queryVagueCompany::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.QUERY_COMPANY_RELATION_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 公司是否存在
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo existsCompany(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.existsCompany(args);
		} catch (Exception e) {
			logger.error("nsy-auth::existsCompany::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.QUERY_COMPANY_INFO_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 加入公司
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo joinCompany(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.joinCompany(args);
		} catch (Exception e) {
			logger.error("nsy-auth::joinCompany::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.JOIN_COMAPNY_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 加入公司审批
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo joinAuditPass(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.joinAuditPass(args);
		} catch (Exception e) {
			logger.error("nsy-auth::joinAuditPass::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.JOIN_COMPANY_AUDIT_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 离开公司
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo exitCompany(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.exitCompany(args);
		} catch (Exception e) {
			logger.error("nsy-auth::exitCompany::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.EXIT_COMPANY_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 查询组织架构
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo queryOrganizaStruct(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.queryOrganizaStruct(args);
		} catch (Exception e) {
			logger.error("nsy-auth::queryOrganizaStruct::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.QUERY_ORGANIZA_STRUCT_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 添加部门
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo addDepartment(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.addDepartment(args);
		} catch (Exception e) {
			logger.error("nsy-auth::addDepartment::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.ADD_DEPARTMENT_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 删除部门,物理删除
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo deleteDepartment(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.deleteDepartment(args);
		} catch (Exception e) {
			logger.error("nsy-auth::deleteDepartment::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.DELETE_DEPARTMENT_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 部门重命名
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo renameDepartment(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.renameDepartment(args);
		} catch (Exception e) {
			logger.error("nsy-auth::renameDepartment::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.RENAME_DEPARTMENT_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 查询公司信息
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo queryCompanyInfo(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.queryCompanyInfo(args);
		} catch (Exception e) {
			logger.error("nsy-auth::queryCompanyInfo::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.QUERY_COMPANY_INFO_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 修改公司信息
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo resetCompanyInfo(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.resetCompanyInfo(args);
		} catch (Exception e) {
			logger.error("nsy-auth::resetCompanyInfo::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.QUERY_COMPANY_INFO_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 添加收发货地址
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo addDeliverAddress(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.addDeliverAddress(args);
		} catch (Exception e) {
			logger.error("nsy-auth::addDeliverAddress::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.ADD_DELIVER_ADDRESS_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 修改收发货地址
	 * @param args
	 * @param response
	 * @return
	 */
	private ReturnPojo updateDeliverAddress(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.updateDeliverAddress(args);
		} catch (Exception e) {
			logger.error("nsy-auth::updateDeliverAddress::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.UPDATE_DELIVER_ADDRESS_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 检查app版本
	 * @param args
	 * @param response
	 */
	private ReturnPojo checkAppVersion(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		ReturnPojo rpLogin = logService.loadToken(args);
		String recode = rpLogin.getReturnCode();
		if(!"200".equals(recode)){
			rp.setReturnCode(rpLogin.getReturnCode());
			rp.setReturnMsg(rpLogin.getReturnMsg());
			return rp;
		}
		LoginLog loginLog =  JSONObject.parseObject(JSONObject.toJSONString(rpLogin.getResult()), LoginLog.class);
		String token = loginLog.getToken();
		if(StringUtils.isEmpty(token)){
			rp.setReturnCode(ReturnCodes.TOKEN_INVALID.getCode());
			rp.setReturnMsg(ReturnCodes.TOKEN_INVALID.getMessage());
			return rp;
		}
		logger.info("nsy-auth::checkAppVersion::get token " + token);
		String sign = null;
		try {
			sign = TokenUtils.getSign(token, args.getTimestamp(), args.getNonce());
		} catch (NoSuchAlgorithmException e) {
			logger.info("nsy-auth::checkAppVersion::" + e.getMessage());
			rp.setReturnCode(ReturnCodes.SIGN_MAKE_FAIL.getCode());
			rp.setReturnMsg(ReturnCodes.SIGN_MAKE_FAIL.getMessage());
			return rp;
		}
		logger.info("nsy-auth::checkAppVersion::get sign " + sign);
		if(sign.equals(args.getSign())){
			String version = PropertiesUtil.getInstance().getProperties("/app.properties").getProperty("version");
			String description = PropertiesUtil.getInstance().getProperties("/app.properties").getProperty("description");
			logger.info("nsy-auth::checkAppVersion::app version " + version);
			logger.info("nsy-auth::checkAppVersion::app description " + description);
			JSONObject json = new JSONObject();
			json.put("version", version);
			json.put("description", description);
			rp.setResult(json);
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		}else{
			rp.setReturnCode(ReturnCodes.SIGN_VALID_FAIL.getCode());
			rp.setReturnMsg(ReturnCodes.SIGN_VALID_FAIL.getMessage());
		}

		return rp;
	}
	
	/**
	 * 找回密码
	 * @param args registerNum/registerType/pwd/oldpwd
	 * @return
	 */
	private ReturnPojo findPwd(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.findPwd(args);
		} catch (Exception e) {
			logger.error("nsy-auth::findPwd::error::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.RESET_PWD_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}

	private ReturnPojo resetPwd(ArgsPojo args, HttpServletResponse response){
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.resetPwd(args);
		} catch (Exception e) {
			logger.error("nsy-auth::resetPwd::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.RESET_PWD_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	public ReturnPojo downLoad(ArgsPojo args, HttpServletResponse response) {
		ReturnPojo rp = new ReturnPojo();
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		try {
			rp = userService.downLoad(args);
		} catch (Exception e) {
			logger.error("nsy-auth::downLoad::" + e.getMessage());
			ReturnCode rc = userService.getReturnCode(ReturnCodes.RESET_USER_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
}
